package com.sansecy.pathmeasure.loadView;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.sansecy.pathmeasure.R;

public class GetSegmentView extends View {

    private PathMeasure pathMeasure;
    private Path dstPath;
    private Path circlePath;
    private float currentAnimatedValue;
    private Paint paint;
    private float start;
    private Bitmap mBitmap;
    private Matrix mMatrix;

    public GetSegmentView(Context context) {
        this(context, null);
    }

    public GetSegmentView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        setLayerType(LAYER_TYPE_SOFTWARE, null);


        mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.arrow_right);
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(4);
        paint.setColor(Color.BLACK);
        paint.setAntiAlias(true);

        dstPath = new Path();
        circlePath = new Path();
        circlePath.addCircle(300, 300, 300, Path.Direction.CW);

//        Log.d(this.getClass().getName(), "GetSegmentView:2 * Math.PI * 30 " + 2 * Math.PI * 300);

        pathMeasure = new PathMeasure(circlePath, true);
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                currentAnimatedValue = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
        valueAnimator.setDuration(2000);
        valueAnimator.start();
//        ValueAnimator startPositionAnimator = ValueAnimator.ofFloat(0, 1);
//        startPositionAnimator.setRepeatCount(ValueAnimator.INFINITE);
//        startPositionAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                start = (float) animation.getAnimatedValue();
////                invalidate();
//            }
//        });
//        startPositionAnimator.setDuration(4000);
//        startPositionAnimator.start();
        mMatrix = new Matrix();
    }

    private float[] pos = new float[2];
    private float[] tan = new float[2];

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        canvas.drawColor(Color.WHITE);
//        float stop = pathMeasure.getLength() * currentAnimatedValue;
//        dstPath.reset();
//        pathMeasure.getSegment(0, stop, dstPath, true);
//        canvas.drawPath(dstPath, paint);

        float length = pathMeasure.getLength();
//        Log.d(this.getClass().getName(), "onDraw:length "+length);
        float stop = length * currentAnimatedValue;
        float start = (float) (stop - ((0.5 - Math.abs(currentAnimatedValue - 0.5)) * length));
//        float start = this.start * length;
        dstPath.reset();
        pathMeasure.getSegment(start, stop, dstPath, true);
        canvas.drawPath(dstPath, paint);

//        pathMeasure.getPosTan(stop, pos, tan);
//        float degrees = (float) (Math.atan2(tan[1], tan[0]) * 180 / Math.PI);
//        mMatrix.reset();
//        mMatrix.postRotate(degrees, mBitmap.getWidth() / 2, mBitmap.getHeight() / 2);
//        mMatrix.postTranslate(pos[0] - mBitmap.getWidth() / 2, pos[1] - mBitmap.getHeight() / 2);
//        canvas.drawBitmap(mBitmap, mMatrix, paint);


        mMatrix.reset();
        pathMeasure.getMatrix(stop, mMatrix, PathMeasure.POSITION_MATRIX_FLAG | PathMeasure.TANGENT_MATRIX_FLAG);
        mMatrix.preTranslate(-mBitmap.getWidth() / 2, -mBitmap.getHeight() / 2);
        canvas.drawBitmap(mBitmap, mMatrix, paint);
    }

    public static void main(String[] args) {
        double v = Math.sqrt(3) / 3;
        System.out.println(v);
        double v1 = (1d / 2d) / (Math.sqrt(3) / 3);
        System.out.println(v1);

    }
}
